import numpy as np
from module.Configuration import FoodConfig
from module.Transmission_data_feishu import insert_data_to_intake_table,query_bitable_records_with_requests

# 实例化FoodConfig类
food_config = FoodConfig()



# 计算BMI指数
def BMI(height, weight):
    """
    计算BMI指数
    :param height: 身高(cm)
    :param weight: 体重(kg)
    :return:
    """
    BMI = height / weight ** 2
    return np.round(BMI, 2)


# 通过食物的克重计算卡路里及营养成分（支持多食物名匹配同一英文）
def kcal_from_weight(weight:float, food_name:str):
    """
    通过食物的克重计算卡路里、脂肪、蛋白质、碳水化合物的含量
    :param weight: 食物的克重
    :param food_name: 食物名称
    :return: 热量卡路里、脂肪、蛋白质、碳水化合物的含量
    """
    # 读取数据库中的数据
    food_config_data = food_config.get_food_config(food_name)
    if food_config_data:
        # 计算卡路里
        kcal = weight * food_config_data['热量']
        fat_gram = weight * food_config_data['脂肪']
        protein_gram = weight * food_config_data['蛋白质']
        carbohydrate_gram = weight * food_config_data['碳水化合物']

        # 对每个结果进行四舍五入
        kcal = np.round(kcal, 2)
        fat_gram = np.round(fat_gram, 2)
        protein_gram = np.round(protein_gram, 2)
        carbohydrate_gram = np.round(carbohydrate_gram, 2)

        # 发送到飞书
        insert_data_to_intake_table(kcal, fat_gram, protein_gram, carbohydrate_gram, food_name, weight)
        return np.round(kcal, 2), np.round(fat_gram, 2), np.round(protein_gram, 2), np.round(carbohydrate_gram, 2)
    # 找不到对应食物
    return None, None, None, None


# 计算每天应该摄入多少克脂肪、蛋白质、碳水化合物
def daily_nutrient_intake(kcal_consumed=2700, kcal_deficit=500):
    """
    计算每天应该摄入多少克脂肪、蛋白质、碳水化合物
    :param kcal_consumed:  已消耗的卡路里 单位：kcal
    :param kcal_deficit:   卡路里缺口 单位：kcal
    :return:
    """
    # 计算每天摄入的脂肪、蛋白质、碳水化合物
    fat_gram_per_day = (kcal_consumed - kcal_deficit) * 0.3
    protein_gram_per_day = (kcal_consumed - kcal_deficit) * 0.3
    carbohydrate_gram_per_day = (kcal_consumed - kcal_deficit) * 0.4

    # 换算成每天摄入的克数：kcal→g
    fat_gram_per_day = fat_gram_per_day / 9
    protein_gram_per_day = protein_gram_per_day / 4
    carbohydrate_gram_per_day = carbohydrate_gram_per_day / 4

    # 对每个结果进行四舍五入
    fat_gram_per_day = np.round(fat_gram_per_day, 2)
    protein_gram_per_day = np.round(protein_gram_per_day, 2)
    carbohydrate_gram_per_day = np.round(carbohydrate_gram_per_day, 2)
    print(
        f"按照热量{kcal_consumed - kcal_deficit},每天应该摄入{fat_gram_per_day}克脂肪、{protein_gram_per_day}克蛋白质、{carbohydrate_gram_per_day}克碳水化合物")

    return kcal_consumed - kcal_deficit,fat_gram_per_day, protein_gram_per_day, carbohydrate_gram_per_day

# 对比已摄入热量
def compare_kcal_consumed():
    """
    对比已摄入热量
    :return: total_kcal, total_carbohydrate_gram, total_protein_gram, total_fat_gram
    """
    records = query_bitable_records_with_requests()
    # 计算各类营养素的总值
    total_kcal = 0
    total_carbohydrate_gram = 0
    total_protein_gram = 0
    total_fat_gram = 0

    for record in records:
        total_kcal += record['fields']['热量(kcal)']
        total_carbohydrate_gram += record['fields']['碳水化合物（g）']
        total_protein_gram += record['fields']['蛋白质（g）']
        total_fat_gram = record['fields']['脂肪(g)']

    print(f"本日热量摄入总量: {total_kcal} kcal")
    print(f"本日碳水化合物总量: {total_carbohydrate_gram} g")
    print(f"本日蛋白质总量: {total_protein_gram} g")
    print(f"本日脂肪总量: {total_fat_gram} g")

    return np.round(total_kcal, 2), np.round(total_carbohydrate_gram, 2), np.round(total_protein_gram, 2), np.round(total_fat_gram, 2)


